﻿using System;
using KinonekoSoftware.CodariaDB.Documents.Theories.Rarities;
using KinonekoSoftware.CodariaDB.Languages;
using KinonekoSoftware.Foundation.UI.Media;

namespace KinonekoSoftware.Studio.Pages.Theories.Rarities
{
    public sealed partial class NewRarityViewModel : AsyncObjectDialog<Rarity>
    {
        public static Task<Result<Rarity>> Create()
        {
            return Dialog<Rarity>(new NewRarityViewModel());
        }

        public static Task<Result<Rarity>> Edit(Rarity element)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Value, element);


            return Dialog<Rarity>(new NewRarityViewModel(), context);
        }

        private Rarity _element;

        private string _background;
        private string _foreground;
        private string _stroke;
        private string _name;
        private string _intro;
        private int    _level;

        public NewRarityViewModel()
        {
            ResetIconCommand   = CreateCommand(DoResetIconCommand);
            ResetImageCommand  = CreateCommand(DoResetImageCommand);
            SelectIconCommand  = CreateCommand(DoSelectIconCommand);
            SelectImageCommand = CreateCommand(DoSelectImageCommand);
        }

        private void Initialize()
        {
            Level           = 1;
            BackgroundColor = MediaFactory.ToColor(0xff, 0x96, 0x00);
            ForegroundColor = MediaFactory.ToColor(0xff, 0xff, 0xff);
            StrokeColor     = MediaFactory.ToColor(0xff, 0xab, 0x12);
        }

        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                _element        = context.GetObject<Rarity>(ContextBase.Key_Value);
                Name            = _element.Name;
                Intro           = _element.Intro;
                Level           = _element.Level;
                Background      = _element.Background;
                Foreground      = _element.Foreground;
                Stroke          = _element.Stroke;
                BackgroundColor = MediaFactory.ToColor(_element.Background);
                ForegroundColor = MediaFactory.ToColor(_element.Foreground);
                StrokeColor     = MediaFactory.ToColor(_element.Stroke);

                GetBitmap(_element.Image, x => Image = x);
                GetBitmap(_element.Icon, x => Icon   = x);

                return true;
            }

            Initialize();
            return false;
        }

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(
                      DialogRule.Create<NewRarityViewModel>(
                                                            CodariaSR.Text_Parameter_EmptyName, x => !string.IsNullOrEmpty(x.Name)));
        }


        public string Name
        {
            get => _name;
            set => TryFinishAndSetValue(ref _name, value);
        }

        public string Intro
        {
            get => _intro;
            set => SetValue(ref _intro, value);
        }


        public string Stroke
        {
            get => _stroke;
            set => SetValue(ref _stroke, value);
        }

        public int Level
        {
            get => _level;
            set => SetValue(ref _level, value);
        }

        public string Foreground
        {
            get => _foreground;
            set => SetValue(ref _foreground, value);
        }

        public string Background
        {
            get => _background;
            set => SetValue(ref _background, value);
        }

        public ICommandEX SelectIconCommand  { get; }
        public ICommandEX ResetIconCommand   { get; }
        public ICommandEX SelectImageCommand { get; }
        public ICommandEX ResetImageCommand  { get; }
    }
}